package jiandian;

import util.ListNode;

/**
 * todo
 *
 * @author: luolidong
 * @create: 2021-07-08 15:12
 * @version: 1.0
 */
public class Demo2 {
    public boolean isPalindrome(ListNode head) {
        ListNode head1=head;
        // 快慢指针
        ListNode fast = head, slow = head1;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //后面反转
        ListNode newHead = reverse(slow.next);
        slow.next = null;
        // 遍历两个链表
        while (head1 != null && newHead != null) {
            if (head1.val != newHead.val) return false;
            head1 = head1.next;
            newHead = newHead.next;
        }
        return true;
    }

    public ListNode reverse(ListNode head) {
        ListNode pre = null, cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        Demo2 demo2 = new Demo2();
        ListNode reverse = demo2.reverse(ListNode.build());
        System.out.println(reverse);
        boolean palindrome = demo2.isPalindrome(ListNode.build());
        System.out.println(palindrome);
    }
}
